package org.example.thread.aba;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class ABADemo1 {
       private static AtomicReference<Integer> atomicReference=new AtomicReference(100);

    public static void main(String[] args) {
        System.out.println("===以下是ABA问题的产生===");
        new Thread(()-> {
            atomicReference.compareAndSet(100,101);
            atomicReference.compareAndSet(101,100);
        },"t1").start();

        new Thread(()->{
            //先暂停一秒，保证完成ABA
            try{
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName()+"\t"+atomicReference.compareAndSet(100,2000)+"\t"+atomicReference.get());
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        },"t2").start();
    }

}
